﻿using System;
using System.Windows;
using System.Windows.Input;

namespace OrbitOne.CRMLyncInfoDashboard.Setup
{
    public interface IInstallCommand : ICommand
    {
        IUninstallCommand UninstallCommand { get; set; }
        void Refresh();
        
    }

    public class InstallCommand: Command, IInstallCommand
    {
        public InstallCommand(Controller controller) : base(controller)
        {
        }

        public IUninstallCommand UninstallCommand { get; set; }

        public void Execute(object parameter)
        {
            var dashboardInstaller = (IDashboardInstaller) parameter;
            if (dashboardInstaller.IsInstalled())
            {
                dashboardInstaller.Reinstall();
            }
            else
            {
                dashboardInstaller.Install();
            }
            Refresh();
            m_controller.UninstallCommand.Refresh();
            MessageBox.Show("Done!");
        }

        public void Refresh()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
            m_controller.NotifyPropertyChanged("InstallButtonContent");
        }

        public bool CanExecute(object parameter)
        {
            return m_controller.IsUrlValid();
        }

        public event EventHandler CanExecuteChanged;
    }
}